package com.woniu.tms.exam.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniu.tms.entity.Role;
import com.woniu.tms.entity.Room;
import com.woniu.tms.entity.Score;
import com.woniu.tms.entity.User;
import com.woniu.tms.exam.service.ExamService;
import com.woniu.tms.entity.Classes;
import com.woniu.tms.entity.Course;
import com.woniu.tms.entity.Exam;

@RequestMapping("exam")
@Controller

public class ExamController {

	@Autowired
	ExamService examService;
	Date date=new Date();


	
	
	/**
	 * @return
	 * 跳转学生exam页面
	 */
	@RequestMapping("student/index")
	public String studentIndex() {
		return "WEB-INF/exam/exam2/studentExam";
	}
	
	/**
	 * @param request
	 * @return
	 * 学生exam页面信息
	 */
	@RequestMapping("student/indexDo")
	@ResponseBody
	public Map<String, Object> studentDoIndex(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
//		HttpSession session=request.getSession();
//		User loginUser=(User)session.getAttribute("loginUser");
		
		try {
			Classes classes = examService.findClassByClassId(1);
			List<Exam> examList = examService.findExamByClasses(classes);
			List<Exam> examList2=new ArrayList<Exam>();
			for (Exam exam : examList) {
				if(date.before(exam.getEndtime())) {
					examList2.add(exam);
				}				
			}
			List<Room> roomList = examService.findRoomAll();
			List<Classes> classList = examService.findClassAll();
			List<Course> courseList = examService.findCourseAll();
			map.put("courseList", courseList);
			map.put("roomList", roomList);
			map.put("classList", classList);
			map.put("examList", examList2);
			map.put("result", "ok");

		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "no");
		}

		return map;
	}
	
	
	/**
	 * @return
	 * 跳转班主任exam页面
	 */
	@RequestMapping("classmanager/index")
	public String adminIndex() {
		return "WEB-INF/exam/exam2/classmanagerExam";
	}

	/**
	 * @param request
	 * @return
	 * 班主任exam页面取值
	 */
	@RequestMapping("classmanager/indexDo")
	@ResponseBody
	public Map<String, Object> classmanagerIndex(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
//		HttpSession session=request.getSession();
//		User loginUser=(User)session.getAttribute("loginUser");
		try {
			
			List<Exam> examList = examService.findExamAll();
			
			List<Exam> examList2=new ArrayList<Exam>();
			for (Exam exam : examList) {
				if(date.before(exam.getEndtime())) {
					examList2.add(exam);
				}				
			}
			List<Classes> classList = examService.findClassAll();
			List<Room> roomList = examService.findRoomAll();
			map.put("classList", classList);
			map.put("roomList", roomList);
			map.put("examList", examList2);
			map.put("result", "ok");

		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "no");
		}

		return map;
	}

	
	
	
	
	/**
	 * @return
	 * 跳转教师exam页面
	 */
	@RequestMapping("teacher/index")
	public String teacherIndex() {
		return "WEB-INF/exam/exam2/teacherExam";
	}
	/**
	 * @param request
	 * @return
	 * 教师exam页面取值
	 */
	@RequestMapping("teacher/indexDo")
	@ResponseBody
	public Map<String, Object> teacherIndexDo(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		HttpSession session=request.getSession();
		User loginUser=(User)session.getAttribute("loginUser");
		try {
			List<Classes> classList = examService.findClassByTeacherId(loginUser.getUid());
			List<Exam> examList = examService.findExamByClassesList(classList);
			List<Exam> examList2=new ArrayList<Exam>();
			for (Exam exam : examList) {
				if(date.before(exam.getEndtime())) {
					examList2.add(exam);
				}				
			}			
			List<Room> roomList = examService.findRoomAll();
			map.put("classList", classList);
			map.put("roomList", roomList);
			map.put("examList", examList);
			map.put("result", "ok");

		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "no");
		}

		return map;
	}

	
	
	
	/**
	 * @return
	 * 跳转教学管理exam页面
	 */
	@RequestMapping("teachermanager/index")
	public String teachermanagerIndex() {
		return "WEB-INF/exam/exam2/teachermanagerExam";
	}
	/**
	 * @param request
	 * @return
	 * 教学管理exam页面取值
	 */
	@RequestMapping("teachermanager/indexDo")
	@ResponseBody
	public Map<String, Object> teachermanagerIndexDo(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<Exam> examList = examService.findExamAll();
			List<Classes> classesList = examService.findClassAll();
			List<Room> roomList = examService.findRoomAll();
			map.put("classList", classesList);
			map.put("roomList", roomList);
			map.put("examList", examList);
			map.put("result", "ok");

		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "no");
		}

		return map;
	}

	
	/**
	 * @return
	 * 跳转管理员exam页面
	 */
	@RequestMapping("admin/index")
	public String adminIndex2() {
		return "WEB-INF/exam/exam2/adminExam";
	}
	/**
	 * @param request
	 * @return
	 * 管理员exam页面取值
	 */
	@RequestMapping("admin/indexDo")
	@ResponseBody
	public Map<String, Object> adminIndexDo(HttpServletRequest request) {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<Exam> examList = examService.findExamAll();
			List<Classes> classesList = examService.findClassAll();
			List<Room> roomList = examService.findRoomAll();
			map.put("classList", classesList);
			map.put("roomList", roomList);
			map.put("examList", examList);
			map.put("result", "ok");

		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "no");
		}
		
		return map;
	}
	
	
	/**
	 * @param exam
	 * @return
	 * 增加考试信息
	 */
	@RequestMapping("addExam")
	@ResponseBody
	public Map<String, Object> addExam(Exam exam) {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			examService.addExam(exam);
//			List<Exam> examList = examService.findExamAll();
//			List<Classes> classesList = examService.findClassAll();
//			List<Room> roomList = examService.findRoomAll();
//			map.put("classList", classesList);
//			map.put("roomList", roomList);
//			map.put("examList", examList);
			map.put("result", "ok");

		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "no");
		}
		
		return map;
	}
	
	
	/**
	 * @param eid
	 * @return
	 * 删除考试信息
	 */
	@RequestMapping("deleteExam")
	@ResponseBody
	public Map<String, Object> deleteByEid(Integer eid) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			examService.deleteByEid(eid);
			map.put("result", "ok");

		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "no");
		}
		
		return map;
	}
	
	/**
	 * @param exam
	 * @return
	 * 编辑考试信息
	 */
	@RequestMapping("editExam")
	@ResponseBody
	public Map<String,Object> editExam(Exam exam){
		System.out.println("*******************************");
		System.out.println("ExamController.editExam()");
		System.out.println(exam.getName());
		System.out.println(exam.getEid());
		System.out.println("*******************************");
		Map<String,Object> map=new HashMap<String, Object>();
		try {
			Exam exam2=examService.findExamByEid(exam.getEid());

			map.put("result", "ok");
		}catch(Exception e) {
			e.printStackTrace();
			map.put("result", "no");
		}
		return map;
	}
	
	
	
	/**
	 * @param cname
	 * @param daterange1
	 * @param daterange2
	 * @return
	 * 班级名，日期条件搜索
	 */
	@RequestMapping("searchBy")
	@ResponseBody
	public Map<String,Object> searchBy(String name,String daterange1,String daterange2) {
		Map<String,Object> map=new HashMap<String, Object>();
	  System.out.println("ExamController.searchBy()");
		try {
	
			List<Exam>	examList=examService.findExamByLikeNameAndCidListAndDaterange(name,null,daterange1,daterange2);
			

				
			List<Classes> classesList = examService.findClassAll();
			List<Room> roomList = examService.findRoomAll();
			map.put("classList", classesList);
			map.put("roomList", roomList);
			map.put("examList", examList);
			map.put("result", "ok");

		}catch(Exception e) {
			map.put("result", "no");
			e.printStackTrace();
		}
		
		return map;

	}
	
	
	
	@RequestMapping("searchByCondition")
	@ResponseBody
	public Map<String,Object> searchByCondition(String name,String daterange1,String daterange2,HttpServletRequest request) {
		Map<String,Object> map=new HashMap<String, Object>();
		HttpSession session=request.getSession();
		User loginUser=(User) session.getAttribute("loginUser");
		List<Integer> classIdList=examService.findCidByTeacherId(loginUser.getUid());		 
		try {
	
			List<Exam>	examList=examService.findExamByLikeNameAndCidListAndDaterange(name,classIdList,daterange1,daterange2);
			

				
			List<Classes> classesList = examService.findClassAll();
			List<Room> roomList = examService.findRoomAll();
			map.put("classList", classesList);
			map.put("roomList", roomList);
			map.put("examList", examList);
			map.put("result", "ok");

		}catch(Exception e) {
			map.put("result", "no");
			e.printStackTrace();
		}
		
		return map;

	}

	

}
